package cn.orange.ch05_stack;

import java.util.LinkedList;

/**
 * LC150.逆波兰表达式求值
 */
public class LC150 {
    public int evalRPN(String[] tokens) {
        LinkedList<Integer> stk = new LinkedList<>();
        for (String token : tokens) {
            switch (token) {
                case "+":
                    Integer v1 = stk.pop();
                    Integer v2 = stk.pop();
                    stk.push(v2 + v1);
                    break;
                case "-":
                    Integer v3 = stk.pop();
                    Integer v4 = stk.pop();
                    stk.push(v4 - v3);
                    break;
                case "*":
                    Integer v5 = stk.pop();
                    Integer v6 = stk.pop();
                    stk.push(v6 * v5);
                    break;
                case "/":
                    Integer v7 = stk.pop();
                    Integer v8 = stk.pop();
                    stk.push(v8 / v7);
                    break;
                default:
                    stk.push(Integer.parseInt(token));
                    break;
            }
        }
        return stk.pop();
    }
    public static void main(String[] args) {
        LC150 alg = new LC150();
        System.out.println(alg.evalRPN(new String[]{"2", "1", "+", "3", "*"}));
    }
}
